﻿@extends('layouts.shop_main')

@section('title', '订单管理')

@section('resources')
    @parent
    <style>
        .header{margin: 0 30px 20px 30px;padding: 10px 0;overflow: hidden;border-bottom: 1px solid #eee;}
        .header p{float: left;font-size: 18px;}
        .header a{float: right;padding: 4px 20px;border: 1px solid #9955c6;border-radius: 3px;color: #9955c6;}
        .search{margin: 0 30px;}
        .search li{overflow: hidden;margin: 20px 0;}
        .search li p{float: left;overflow: hidden;}
        .search li p span{float: left;width: 100px;text-align: right;font-size: 14px;color: #808080;line-height: 25px;padding-right: 10px;}
        .search li p input{float: left;width: 200px;height: 25px;border: 1px solid #eee;border-radius: 3px;padding: 5px;}
        .search li p select{float: left;width: 200px;height: 25px;border: 1px solid #eee;border-radius: 3px;}
        .search li table{border-top: 1px solid #DEE3EA;border-bottom: 1px solid #DEE3EA;}
        .search li table tbody tr:nth-child(2n+1){background-color: #F3F3F3;}
        .search li table td{text-align: center;font-size: 14px;padding: 10px 0;color: #808080;}
        .search li table th{text-align: center;font-size: 16px;padding: 10px 0;}
        #last_page{background-color: #9955c6;float: left;width: 100px;height: 35px;color: #fff;display: none;border: none;}
        #next_page{background-color: #9955c6;float: right;width: 100px;height: 35px;color: #fff;display: none;border: none;}
        .refresh{float: right;width: 90px;background: url({{env('IMAGE_DOMAIN')}}images/shop/bgs/refresh.png) 5px 3px no-repeat;text-align: center;font-size: 16px;color: #9955c6;}
        .order_consume{float: left;}
    </style>
@endsection

@section('content')
	<div class="content">
        <div class="header">
            <p>订单管理</p>
            <a href="#" onclick="export_data()">导出</a>
        </div>
        <ul class="search">
            <li class="search_tip1">
                <p>
                    <span>订单号：</span>
                    <input type="text" name="ordernumber" value="">
                </p>
                <p>
                    <span>消费金额：</span>
                    <input type="text" name="minamount" value="">
                    <span style="width: 40px;text-align: center;">-</span>
                    <input type="text" name="maxamount" value="">
                </p>
            </li>
            <li class="search_tip2">
                <p>
                    <span>收银员：</span>
                    <select name="shopuserid" id="shopuserid">
                    @foreach($shopuserlists as $list)
                        <option value="{{$list['shopuserid']}}">{{$list['shopusername']}}</option>
                    @endforeach
                    </select>
                </p>
                <p>
                    <span>订单时间：</span>
                    <input type="date" name="starttime" value="">
                    <span style="width: 40px;text-align: center;">-</span>
                    <input type="date" name="lasttime" value="">
                </p>
            </li>
            <li class="search_tip3">
                <p>
                    <span>结款状态：</span>
                    <select name="status" id="status">
                        <option value="%">全部</option>
                        <option value="1">已提现</option>
                        <option value="0">未提现</option>
                    </select>
                </p>
                <p style="float: right;">
                    <input style="width: 100px;background-color: #9955c6;color: #fff;height: 30px;" type="button" name="search_all" value="搜索">
                </p>
            </li>
            <li>
                <span class="order_consume"></span>
                <a class="refresh" href="">刷新</a>
            </li>
            <li>
                <table style="width: 100%;" cellpadding="10">
                    <thead>
                        <tr>
                            <th>订单时间</th>
                            <th>订单号</th>
                            <th>用户手机号</th>
                            <th>消费金额</th>
                            <th>结款状态</th>
                            <th>消费门店</th>
                            <th>收银员</th>
                        </tr>
                    </thead>
                    <tbody id="order_message">

                    </tbody>
                </table>
                <div style="margin-top: 20px;overflow: hidden;">
                    <input id="last_page" type="button" name="" value="上一页">
                    <input id="next_page" type="button" name="" value="下一页">
                </div>
            </li>
        </ul>   
    </div>
@endsection

@section('scriptResources')
    @parent
    <script>
        var ordernumber = $("input[name='ordernumber']").val();
        var minamount = $("input[name='minamount']").val();
        var maxamount = $("input[name='maxamount']").val();
        var starttime = $("input[name='starttime']").val();
        var lasttime = $("input[name='lasttime']").val();
        var shopuserid = $("#shopuserid").val();
        var shopaccountid = '{{$shopaccountid}}';
        var status = $("#status").val();
        var page = 1;
        var paginate = 10;
        if(shopaccountid != 0) {
            //从结款过来
            $('.search_tip1').hide();
            $('.search_tip2').hide();
            $('.search_tip3').hide();
        }
        function order(page,ordernumber,minamount,maxamount,starttime,lasttime,shopuserid,status){
            $.ajax({
                url : "/shop/order/list",
                type : "get",
                data :{
                    ordernumber:ordernumber,
                    minamount:minamount,
                    maxamount:maxamount,
                    starttime:starttime,
                    lasttime:lasttime,
                    shopuserid:shopuserid,
                    status:status,
                    page:page,
                    paginate:paginate,
                    shopaccountid:shopaccountid
                },
                dataType : "json",
                success:function(data){
                    if(data.status == "success"){
                        $("#order_message").empty();
                        var data = data.data;
                        var lists = data.lists;
                        var money_sun = data.sum;
                        if(money_sun == null){
                            money_sun = 0;
                        }

                        $(".order_consume").html('合计：'+data.count+' 笔订单  消费金额 '+money_sun);
                        if(lists.last_page <= page){
                            $("#next_page").hide();
                        }else{
                            $("#next_page").show();
                        }
                        if(page <=1){
                            $("#last_page").hide();
                        }else{
                            $("#last_page").show();
                        }
                        for (var i = 0; i < lists.data.length; i++) {
                            var tr = $('<tr>'+
                                        '<td>'+lists.data[i].created_at+'</td>'+
                                        '<td><a href="/shop/order/details?orderid='+lists.data[i].orderid+'" style="color: #1155CC;">'+lists.data[i].ordernumber+'</a></td>'+//TODO
                                        '<td>'+lists.data[i].mobile+'</td>'+
                                        '<td>'+lists.data[i].orderamount+'</td>'+
                                        '<td>'+(lists.data[i].accountstatus==0?"未提现":"已提现")+'</td>'+
                                        '<td>'+lists.data[i].shopname+'</td>'+
                                        '<td>'+lists.data[i].shopusername+'</td>'+
                                        '</tr>');
                            $('#order_message').append(tr);
                        };
                    }else{
                        alert(data.message)
                    }
                }
            });
        }
        order(1,ordernumber,minamount,maxamount,starttime,lasttime,shopuserid,status)
        $("input[name='search_all']").click(function(){
            ordernumber = $("input[name='ordernumber']").val();
            minamount = $("input[name='minamount']").val();
            maxamount = $("input[name='maxamount']").val();
            starttime = $("input[name='starttime']").val();
            lasttime = $("input[name='lasttime']").val();
            shopuserid = $("#shopuserid").val();
            status = $("#status").val();
            if(starttime == 0){
                starttime = "2016-07-01";
            }
            if(lasttime == 0){
                var now_date = new Date();
                var year = now_date.getFullYear();
                var month = now_date.getMonth()+1;
                var date = now_date.getDate();
                lasttime = year + "-" + month + "-" + date;
            }
            page = 1;
            order(page,ordernumber,minamount,maxamount,starttime,lasttime,shopuserid,status)
        })
        //下一页
        $("#next_page").click(function(){
            page++;
            order(page,ordernumber,minamount,maxamount,starttime,lasttime,shopuserid,status);
        })
        //上一页
        $("#last_page").click(function(){
            page--;
            order(page,ordernumber,minamount,maxamount,starttime,lasttime,shopuserid,status);
        })
        //导出
        function export_data() {
            var order_number = $("input[name='ordernumber']").val();
            var min_amount = $("input[name='minamount']").val();
            var max_amount = $("input[name='maxamount']").val();
            var start_time = $("input[name='starttime']").val();
            var last_time = $("input[name='lasttime']").val();
            var shop_user_id = $("#shopuserid").val();
            var status = $("#status").val();
            window.open("/shop/order/export?starttime="+start_time+"&lasttime="+last_time+"&ordernumber="+order_number+"&minamount="+min_amount+"&maxamount="+max_amount+"&shopuserid="+shop_user_id+"&status="+status+'&shopaccountid='+shopaccountid, '_blank');
        }
    </script>
@endsection